package projsa.ui;

import org.openqa.selenium.firefox.FirefoxDriver;
import projsa.modelsa.SearchCriteria;
import projsa.modelsa.TransUtils;
import projsa.reservation.CityConstants;
import projsa.reservation.CityHours;
import projsa.reservation.ReservationMaker;
import projsa.reservation.WebUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;

public class MainWindow extends JFrame {

    private JPanel panelForCheckboxes;
    private JComboBox fromCity;
    private JComboBox toCity;
    private JTextField dateTextField;
    private JTextField refreshRateTextField;

    public MainWindow(String title) throws HeadlessException {
        super(title);
        setSize(400, 400);

        createContent();
    }

    private void createContent() {
        final GridLayout layout = new GridLayout(2, 1);
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(7, 2));
        add(mainPanel);
        setLayout(layout);


        mainPanel.add(new JLabel("Datum"));
        dateTextField = new JTextField();
        mainPanel.add(dateTextField);

        mainPanel.add(new JLabel("Od - do"));

        fromCity = new JComboBox(CityConstants.getAll());
        mainPanel.add(fromCity);

        fromCity.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                final Object item = e.getItem();
                refreshPanelForCheckboxes((String) item);
                panelForCheckboxes.updateUI();
            }
        });

        toCity = new JComboBox(CityConstants.getAll());
        mainPanel.add(toCity);

        refreshRateTextField = new JTextField();
        refreshRateTextField.setText("5");
        mainPanel.add(refreshRateTextField);

        final JButton reservationButton = new JButton("REZERVOVAT");
        mainPanel.add(reservationButton);

        reservationButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                provideReservation();
            }
        });

        panelForCheckboxes = new JPanel();
        panelForCheckboxes.setLayout(new GridLayout(15, 1));
        add(panelForCheckboxes);
    }

    private void provideReservation() {
        final ReservationMaker reservationMaker = new ReservationMaker(new FirefoxDriver());

        try {
            final int i = Integer.parseInt(refreshRateTextField.getText());
            WebUtils.request = i;
        } catch (Exception e) {
            e.printStackTrace();
        }

        final List<Integer> integers = new ArrayList<Integer>();

        final Component[] components = panelForCheckboxes.getComponents();
        for (final Component component : components) {
            final JCheckBox jCheckBox = (JCheckBox) component;
            if (jCheckBox.isSelected()) {
                final String text = jCheckBox.getText();
                integers.add(TransUtils.stringToMinuteOfDay(text));
            }
        }

        final SearchCriteria searchCriteria = new SearchCriteria(dateTextField.getText(),
                fromCity.getSelectedItem().toString(), toCity.getSelectedItem().toString(),
                integers.toArray(new Integer[0])
        );

        try {
            reservationMaker.makeReservation(searchCriteria);
        } catch (Exception e) {
            System.exit(0);
        }
    }

    public void refreshPanelForCheckboxes(String item) {
        panelForCheckboxes.removeAll();

        final List<Integer> hoursForCity = CityHours.getHoursForCity(item);
        for (Integer integer : hoursForCity) {
            final String text = integer / 60 + ":" + integer % 60;
            panelForCheckboxes.add(new JCheckBox(text));
        }

    }


}
